/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spakehelpdesk.session;

import com.spakehelpdesk.entity.Complaint;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.jms.*;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author leehoanh
 */
@Stateless
@LocalBean
public class ComplaintSession {

    @Resource(mappedName = "jms/Notification/Queue")
    private Queue notificationQueue;
    @Resource(mappedName = "jms/Notification/QueueFactory")
    private ConnectionFactory notificationQueueFactory;
    @PersistenceContext(unitName = "Spake_Helpdesk-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    //select all complaint in company
    public List<Complaint> getAll() {
        Query query = em.createNamedQuery("Complaint.findAll");
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }

    }
    //Method get complaint by depatmentID

    public List<Complaint> getComplaintInDepart(int departId) {
        Query query = em.createNamedQuery("Complaint.findByDepartment");
        query.setParameter("departmentID", departId);
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
    //Method get Complaint by depatmentId

    public Complaint getComplaintByComplaintId(int complId) {
        Query query = em.createNamedQuery("Complaint.findByComplaintID");
        query.setParameter("complaintID", complId);
        try {
            return (Complaint) query.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }
    //select all complaint sort by departnemt

    public List<Complaint> getAllSortByDepartment() {
        Query query = em.createNamedQuery("Complaint.sortWithDepartment");
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }

    }
    //Method get Complaint by depatmentNo

    public Complaint getComplaintByComplaintNo(String complNo) {
        Query query = em.createNamedQuery("Complaint.findByComplaintNo");
        query.setParameter("complaintNo", complNo);
        try {
            return (Complaint) query.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }
    //Method get Complaint is created between date1 and date2

    public List<Complaint> getComplaintBetweenDateLodg(Date dateLodge1, Date dateLodge2) {
        Query query = em.createNamedQuery("Complaint.findByDateLodgBetween");
        query.setParameter("dateLodge1", dateLodge1);
        query.setParameter("dateLodge2", dateLodge2);
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
     //select all complaint sort by dateLodg

    public List<Complaint> getAllSortWithDateLodg() {
        Query query = em.createNamedQuery("Complaint.sortWithDateLodg");
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }


    }
    //Update or edit a complaint

    public Complaint updateAccount(Complaint complaint) {
        Complaint updated = em.merge(complaint);
        try {
            sendJMSMessageToQueue(updated);//send task update to queue
        } catch (JMSException ex) {
            Logger.getLogger(AccountSession.class.getName()).log(Level.SEVERE, null, ex);
        }
        return updated;
    }
    //create a new Account

    public void createNewAccount(Complaint complaint) {

        em.persist(complaint);//create

    }
    //Method get complaint by categoryId

    public List<Complaint> getComplaintByCategory(int categoryId) {
        Query query = em.createNamedQuery("Complaint.findByCategory");
        query.setParameter("departmentID", categoryId);
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
    //select all complaint sort by category

    public List<Complaint> getAllSortByCategory() {
        Query query = em.createNamedQuery("Complaint.sortWithCategory");
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }

    }
    //select all complaint don't  have Technicain

    public List<Complaint> getComplaintTechnicainIsNull() {
        Query query = em.createNamedQuery("Complaint.findByTechnicainIsNull");
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }

    }
    //select all complaint don't  set Priority

    public List<Complaint> getComplaintPriorityIsNull() {
        Query query = em.createNamedQuery("Complaint.findByPriorityIsNull");
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }

    }
    //Method get Complaint by status and date lodg

    public List<Complaint> getComplaintByStatusAndDateLodg(int statusIs, Date dateLodge1, Date dateLodge2) {
        Query query = em.createNamedQuery("Complaint.findByStatusAndDateLodg");
        query.setParameter("statusId", statusIs);
        query.setParameter("dateLodge1", dateLodge1);
        query.setParameter("dateLodge2", dateLodge2);
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
    //Method get Complaint by status

    public List<Complaint> getComplaintByStatus(String statusId) {
        Query query = em.createNamedQuery("Complaint.findByStatus");
        query.setParameter("statusId", statusId);
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
    //select all complaint sort by status

    public List<Complaint> getAllSortWithStatus() {
        Query query = em.createNamedQuery("Complaint.sortWithStatus");
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }

    }
    //Method get Complaint by Priority

    public List<Complaint> getComplaintByPriority(int priorityID) {
        Query query = em.createNamedQuery("Complaint.findByPriority");
        query.setParameter("priorityID", priorityID);
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
    //select all complaint sort by priority

    public List<Complaint> getAllSortWithPriority() {
        Query query = em.createNamedQuery("Complaint.sortWithPriority");
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }

    }
    //Method get Complaint by Creater

    public List<Complaint> getComplaintByAccount(String accountID) {
        Query query = em.createNamedQuery("Complaint.findCompOfAccount");
        query.setParameter("accountID", accountID);
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
    //select all complaint sort by priority

    public List<Complaint> getAllSortWithAccount() {
        Query query = em.createNamedQuery("Complaint.sortWithAccount");
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }


    }
    //Method get Complaint by Creater

    public List<Complaint> getComplaintByTechnicain(String technicantId) {
        Query query = em.createNamedQuery("Complaint.findCompOfTechnicain");
        query.setParameter("accountID", technicantId);
        try {
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }
    }

    private Message createJMSMessageForjmsNotificationQueue(Session session, Object messageData) throws JMSException {
        // TODO create and populate message to send
        TextMessage tm = session.createTextMessage();
        tm.setText(messageData.toString());
        return tm;
    }

    private void sendJMSMessageToQueue(Object messageData) throws JMSException {
        Connection connection = null;
        Session session = null;
        try {
            connection = notificationQueueFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(notificationQueue);
            messageProducer.send(createJMSMessageForjmsNotificationQueue(session, messageData));
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}
